package GUI; //done

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.SystemColor;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import Client.ConnectionToServer;
import Entities.Customer;
import Entities.CustomerContactEmployee;

/**
 * This Class controls Add Customers Interest Window.
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public class AddCustomerInterestWindow extends JPanel{
	/** The serial Version UID. */
	private static final long serialVersionUID = 1L;

	/** The Category ComboBox. */
	private JComboBox CategoryComboBox = null;

	/** The Category Label. */
	private JLabel CategoryLabel;
	
	/** The Background Label. */
	private JLabel BackgroundLabel;
	
	/** The Category ID. */
	private int CategoryID = 0;
	
	/** The Customer Interests array. */
	private String[] CustomerInterests = null;
	
	/** The customer contact employee user. */
	private CustomerContactEmployee customercontactemployee  = null;

	/** The Save Button. */
	private JButton btnSaveInterest;
	
	/** The Delete Button. */
	private JButton btnDeleteInterest;
	
	/** The Load Interests Button. */
	private JButton btnLoadInterests;

	/** The Customer ID Label. */
	private JLabel custIDLabel;

	/** The Customer Name Label. */
	private JLabel custNameLabel;

	/** The Customer Age Label. */
	private JLabel custAgeLabel;

	/** The Customer Address Label. */
	private JLabel custAddressLabel;

	/** The Customer Phone Label. */
	private JLabel custPhoneLabel;

	/** The Customer Importance Label. */
	private JLabel custImportanceLabel;

	/** The Customer to add Interests to. */
	private Customer customer;

	/** The Customer ID Text Field. */
	private JTextField custIDTextField;

	/** The Customer Name Text Field. */
	private JTextField custNameTextField;

	/** The Customer Age Text Field. */
	private JTextField custAgeTextField;

	/** The Customer Address Text Field. */
	private JTextField custAddressTextField;

	/** The Customer Phone Text Field. */
	private JTextField custPhoneTextField;

	/** The Customer Importance Text Field. */
	private JTextField custImportanceTextField;

	/** The Category Products Label. */
	private JLabel allCategoryProductsLabel;

	/** The Customer Interests Label. */
	private JLabel allCustomerInterestsLabel;

	/** The Customer Interests Scroll Pane. */
	private JScrollPane InterestsScrollPane;

	/** The Category Products Scroll Pane. */
	private JScrollPane categoryProductsScrollPane;

	/** The Customer Interests Text Area. */
	private JTextArea allCustomerInterestsTextArea;

	/** The Category Products Text Area. */
	private JTextArea CategoryProductsTextArea;
	

	/**
	 * Default constructor
	 */
	public AddCustomerInterestWindow(){
		super();
		initialize();
	}

	/**
	 * This function initializes AddCustomerInterestWindow
	 */
	private void initialize(){
		custIDLabel = new JLabel();
		custIDLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		custIDLabel.setBounds(new Rectangle(34, 21, 130, 22));
		custIDLabel.setText("Customer ID:");
		
		custNameLabel = new JLabel();
		custNameLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		custNameLabel.setBounds(new Rectangle(34, 55, 130, 22));
		custNameLabel.setText("Customer name:");
		
		custAgeLabel = new JLabel();
		custAgeLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		custAgeLabel.setBounds(new Rectangle(34, 156, 130, 22));
		custAgeLabel.setText("Customer age:");
		
		custAddressLabel = new JLabel();
		custAddressLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		custAddressLabel.setBounds(new Rectangle(34, 89, 130, 22));
		custAddressLabel.setText("Customer address:");
		
		custPhoneLabel = new JLabel();
		custPhoneLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		custPhoneLabel.setBounds(new Rectangle(34, 123, 130, 22));
		custPhoneLabel.setText("Customer phone:");
		
		custImportanceLabel = new JLabel();
		custImportanceLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		custImportanceLabel.setBounds(new Rectangle(34, 189, 130, 22));
		custImportanceLabel.setText("Customer importance:");
		
		allCategoryProductsLabel = new JLabel();
		allCategoryProductsLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		allCategoryProductsLabel.setBounds(new Rectangle(15, 434, 154, 22));
		allCategoryProductsLabel.setText("All Category products:");
		
		allCustomerInterestsLabel = new JLabel();
		allCustomerInterestsLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		allCustomerInterestsLabel.setBounds(new Rectangle(15, 245, 154, 22));
		allCustomerInterestsLabel.setText("All Customer Interests:");
		
		this.add(allCustomerInterestsLabel,null);
		this.add(allCategoryProductsLabel,null);
		this.add(custIDLabel, null);
		this.add(custNameLabel, null);
		this.add(custAgeLabel, null);
		this.add(custAddressLabel, null);
		this.add(custPhoneLabel,null);
		this.add(custImportanceLabel,null);
		this.add(getCIdTextField(), null);
		this.add(getCNameTextField(), null);
		this.add(getCAageTextField(), null);
		this.add(getCAddressTextField(), null);
		this.add(getCPhoneTextField(),null);
		this.add(getCImportanceTextField(),null);
		this.add(getAllCustomerInterestsTextArea());
		this.add(getAllCategoryProductsTextArea());
		
		CategoryLabel = new JLabel();
		CategoryLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		CategoryLabel.setBounds(new Rectangle(15, 341, 179, 22));
		CategoryLabel.setText("Choose Interest Category:");
		
		this.setBounds(200,150,450, 600);
		this.setLayout(null);
		
		InterestsScrollPane = new JScrollPane();
		AdjustmentListener listener = new AdjustmentListener(){
			public void adjustmentValueChanged(AdjustmentEvent evt) {
			}	
		};
		InterestsScrollPane.getHorizontalScrollBar().addAdjustmentListener(listener);
		InterestsScrollPane.getVerticalScrollBar().addAdjustmentListener(listener);
		InterestsScrollPane.setBounds(new Rectangle(181, 225, 248, 98));
		this.add(InterestsScrollPane);
		
		categoryProductsScrollPane = new JScrollPane();
		AdjustmentListener listener1 = new AdjustmentListener(){
			public void adjustmentValueChanged(AdjustmentEvent evt) {
			}	
		};
		categoryProductsScrollPane.getHorizontalScrollBar().addAdjustmentListener(listener1);
		categoryProductsScrollPane.getVerticalScrollBar().addAdjustmentListener(listener1);
		categoryProductsScrollPane.setBounds(new Rectangle(181, 398, 248, 98));
		this.add(categoryProductsScrollPane);
		
		this.add(CategoryLabel, null);
		this.add(getCategoryComboBox(),null);
		this.add(getbtnSaveInterest());
		this.add(getbtnDeleteInterest());
		this.add(getbtnLoadInterests());

		BackgroundLabel = new JLabel("");
		BackgroundLabel.setIcon(new ImageIcon(AddCustomerInterestWindow.class.getResource("/Pictures/bg.png")));
		BackgroundLabel.setBounds(0, 0, 450, 600);
		this.add(BackgroundLabel);
	}
	
	/**
	 * This function initializes custIDTextField	
	 */
	private JTextField getCIdTextField(){
		if (custIDTextField == null) {
			custIDTextField = new JTextField();
			custIDTextField.setBounds(new Rectangle(230, 21, 161, 25));
			custIDTextField.setEditable(false);
		}
		return custIDTextField;
	}
	
	/**
	 * This function initializes custNameTextField	
	 */
	private JTextField getCNameTextField(){
		if (custNameTextField == null) {
			custNameTextField = new JTextField();
			custNameTextField.setBounds(new Rectangle(230, 55, 161, 25));
			custNameTextField.setEditable(false);
		}
		return custNameTextField;
	}
	
	/**
	 * This function initializes custAgeTextField	
	 */
	private JTextField getCAageTextField(){
		if (custAgeTextField == null) {
			custAgeTextField = new JTextField();
			custAgeTextField.setBounds(new Rectangle(230, 156, 161, 25));
			custAgeTextField.setEditable(false);
		}
		return custAgeTextField;
	}
	
	/**
	 * This function initializes cAreaTextField	
	 */
	private JTextField getCAddressTextField(){
		if (custAddressTextField == null) {
			custAddressTextField = new JTextField();
			custAddressTextField.setBounds(new Rectangle(230, 89, 161, 25));
			custAddressTextField.setEditable(false);
		}
		return custAddressTextField;
	}
	
	/**
	 * This function initializes custPhoneTextField	
	 */
	private JTextField getCPhoneTextField(){
		if (custPhoneTextField == null) {
			custPhoneTextField = new JTextField();
			custPhoneTextField.setBounds(new Rectangle(230, 123, 161, 25));
			custPhoneTextField.setEditable(false);
		}
		return custPhoneTextField;
	}
	
	/**
	 * This function initializes custImportanceTextField	
	 */
	private JTextField getCImportanceTextField(){
		if (custImportanceTextField == null) {
			custImportanceTextField = new JTextField();
			custImportanceTextField.setBounds(new Rectangle(230, 189, 161, 25));
			custImportanceTextField.setEditable(false);
		}
		return custImportanceTextField;
	}
	
	/**
	 * This function initializes custImportanceTextField	
	 */
	private JTextArea getAllCategoryProductsTextArea(){
		if (CategoryProductsTextArea == null) {
			CategoryProductsTextArea = new JTextArea();
			CategoryProductsTextArea.setBackground(SystemColor.menu);
			CategoryProductsTextArea.setBounds(new Rectangle(181, 398, 248, 98));
			CategoryProductsTextArea.setEditable(false);
		}
		return CategoryProductsTextArea;
	}
	
	/**
	 * This function initializes custImportanceTextField	
	 */
	private JTextArea getAllCustomerInterestsTextArea(){
		if (allCustomerInterestsTextArea == null) {
			allCustomerInterestsTextArea = new JTextArea();
			allCustomerInterestsTextArea.setBackground(SystemColor.menu);
			allCustomerInterestsTextArea.setBounds(new Rectangle(181, 225, 248, 98));
			allCustomerInterestsTextArea.setEditable(false);
		}
		return allCustomerInterestsTextArea;
	}
	
	/**
	 * This function initializes CategoryComboBox	
	 */
	public JComboBox getCategoryComboBox(){
		if (CategoryComboBox == null){
			CategoryComboBox = new JComboBox();
			CategoryComboBox.setBounds(new Rectangle(204, 341, 138, 25));
			updateCategoryComboBox();			
		}
		return CategoryComboBox;
	}
	
	/**
	 * This function add all sections from data base to CategoryComboBox
	 */
	public void updateCategoryComboBox(){
		String[] CategoryList = null;
		int i=0,itemcount = CategoryComboBox.getItemCount();
		if (itemcount > 0) {
			CategoryComboBox.addItem(":");
			CategoryComboBox.setSelectedItem(":");				
		}
		for (i=0; i < itemcount ; i++ ){				
			CategoryComboBox.removeItemAt(0);						
		}			
		try{
			ConnectionToServer connectiontoserver = new ConnectionToServer();		
			CategoryList = (String[]) connectiontoserver.loadAllCategories();
			if (CategoryList == null){
				JOptionPane.showMessageDialog(null, "Error!");
				CategoryComboBox.addItem(" ");
			}	
			else{
				int len = CategoryList.length;
				if (len == 0)
					CategoryComboBox.addItem(":");	
				else
					CategoryComboBox.addItem(" ");
				for(i=0; i < len; i++){
					CategoryComboBox.addItem(CategoryList[i]);
				}	
			}
		}
		catch(Exception e){
			JOptionPane.showMessageDialog(null, "Error!");
			CategoryComboBox.addItem(" ");
		}
		
		CategoryComboBox.addActionListener(new java.awt.event.ActionListener(){
			public void actionPerformed(java.awt.event.ActionEvent e){
				String[] productList = null;
				try{
					String sectionId = (String)CategoryComboBox.getSelectedItem();
					CategoryID = Integer.parseInt(sectionId.substring(0, sectionId.indexOf(":")));
					ConnectionToServer connectiontoserver = new ConnectionToServer();
					productList = (String[])connectiontoserver.loadCategoryProducts(CategoryID);						
				}
				catch(Exception exc){
					JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
					CategoryProductsTextArea.setText("EMPTY!!!");
					return;
				}
				if (productList != null){	
					int len = productList.length, i=0;
					String str = "";
					for(i=0; i < len;i++){
						str = str+productList[i]+"\n";									
					}
					CategoryProductsTextArea.setText(str);					
				}
				else
					JOptionPane.showMessageDialog(null, "ERROR!!!","",JOptionPane.ERROR_MESSAGE);
			}	
		});	
	}
	
	/**
	 * This function initializes getbtnSaveInterest	
	 */
	private JButton getbtnSaveInterest(){
		if (btnSaveInterest == null) {
			btnSaveInterest = new JButton("Save Interest");
			btnSaveInterest.setBounds(242, 518, 138, 34);
			btnSaveInterest.addActionListener(new java.awt.event.ActionListener(){
				public void actionPerformed(java.awt.event.ActionEvent e){	
					int[] CategoryIDs = null;
					int[] InterestRates = null;
					int CISize = 0, alreadyExists = 0;
					try{
						CISize = CustomerInterests.length;
						CategoryIDs = new int[CISize];
						InterestRates = new int[CISize];
						for(int i = 0; i < CISize ; i++){
							CategoryIDs[i] = Integer.parseInt(CustomerInterests[i].substring(0,CustomerInterests[i].indexOf(":")));
							InterestRates[i] = Integer.parseInt(CustomerInterests[i].substring(CustomerInterests[i].indexOf(":")+1, CustomerInterests[i].indexOf(".")));
							if(CategoryIDs[i] == CategoryID){
								JOptionPane.showMessageDialog(null, "This Category Interest already exists!");
								JOptionPane.showMessageDialog(null, "If you want to update the rate, please delete it first, then add again");
								alreadyExists = 1;
								break;
							}
						}
						if(alreadyExists == 0){
							ConnectionToServer connectiontoserver2 = new ConnectionToServer();
							String msgfromserver = (String)connectiontoserver2.AddCustomerInterest(customer.getCustID(), CategoryID, 1);   
							if (msgfromserver != null){
								JOptionPane.showMessageDialog(null, "The Interest added successfuly");
							}
							else
								JOptionPane.showMessageDialog(null, "Error, Can't add interest!");
						}
					}
					catch (Exception excp){
						JOptionPane.showMessageDialog(null, "Error!");	
					}	
				}				
			});
		}
		return btnSaveInterest;
	}
	
	/**
	 * This function initializes getbtnDeleteInterest	
	 */
	private JButton getbtnDeleteInterest(){
		if (btnDeleteInterest == null) {
			btnDeleteInterest = new JButton("Delete Interest");
			btnDeleteInterest.setBounds(56, 518, 138, 34);
			btnDeleteInterest.addActionListener(new java.awt.event.ActionListener(){
				public void actionPerformed(java.awt.event.ActionEvent e){	
					int[] CategoryIDs = null;
					int[] InterestRates = null;
					int CISize = 0, alreadyExists = 0;
					try{
						CISize = CustomerInterests.length;
						CategoryIDs = new int[CISize];
						InterestRates = new int[CISize];
						for(int i = 0; i < CISize ; i++){
							CategoryIDs[i] = Integer.parseInt(CustomerInterests[i].substring(0,CustomerInterests[i].indexOf(":")));
							InterestRates[i] = Integer.parseInt(CustomerInterests[i].substring(CustomerInterests[i].indexOf(":")+1, CustomerInterests[i].indexOf(".")));
							if(CategoryIDs[i] == CategoryID){
								alreadyExists = 1;
								break;
							}
						}
						if(alreadyExists == 1){
							ConnectionToServer connectiontoserver2 = new ConnectionToServer();
							String msgfromserver = (String)connectiontoserver2.DeleteCustomerInterest(customer.getCustID(), CategoryID);   
							if (msgfromserver != null){
								JOptionPane.showMessageDialog(null, "The Interest deleted successfuly");
							}
							else
								JOptionPane.showMessageDialog(null, "Error, Can't delete interest!");
						}
						else{
							JOptionPane.showMessageDialog(null, "The Interest does not exist!");
						}
					}
					catch (Exception excp){
						JOptionPane.showMessageDialog(null, "Error!");	
					}	
				}
			});
		}
		return btnDeleteInterest;
	}
	
	/**
	 * This function initializes getbtnLoadInterests	
	 */
	private JButton getbtnLoadInterests(){
		if (btnLoadInterests == null) {
			btnLoadInterests = new JButton("Load Interests");
			btnLoadInterests.setBounds(15, 278, 138, 34);
			btnLoadInterests.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						ConnectionToServer connectiontoserver = new ConnectionToServer();
						CustomerInterests = (String[])connectiontoserver.LoadAllCustomerInterests(customer.getCustID());					
					}
					catch(Exception exc){
						JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
						allCustomerInterestsTextArea.setText("EMPTY!!!");
					}
					if (CustomerInterests != null){	
						int len = CustomerInterests.length, i=0;
						String str = "";
						for(i=0; i< len;i++){
							str = str+"Category ID: "+
						Integer.parseInt(CustomerInterests[i].substring(0,CustomerInterests[i].indexOf(":")))+ ".   Rating: " +
						Integer.parseInt(CustomerInterests[i].substring(CustomerInterests[i].indexOf(":")+1, CustomerInterests[i].indexOf(".")))+
						".\n";					
						}
						allCustomerInterestsTextArea.setText(str);							
					}
					else
						JOptionPane.showMessageDialog(null, "ERROR!!!","",JOptionPane.ERROR_MESSAGE);
				}
			});
		}
		return btnLoadInterests;
	}
	
	/**
	 * This function loads customer details from the DB
	 * @param customer The requested customer
	 */
	public void loadCustomer(Customer customer){
		this.customer = customer;
		custIDTextField.setText(String.valueOf(customer.getCustID()));
		custNameTextField.setText(customer.getCustName()) ;
		custAgeTextField.setText(String.valueOf(customer.getCustAge()));
		custAddressTextField.setText(String.valueOf(customer.getCustAddressZone()));
		custPhoneTextField.setText(customer.getCustPhone()) ;
		custImportanceTextField.setText(String.valueOf(customer.getImportance()));		
	}
}
